package controller.admin;

import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.TextField;
import sql.JDBC_T;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.MessageFormat;

public class AdminAddCourseController {
    @FXML
    private TextField adm_courseadd_tf_cid;
    @FXML
    private TextField adm_courseadd_tf_cname;
    @FXML
    private TextField adm_courseadd_tf_tid;
    @FXML
    private TextField adm_courseadd_tf_ctime;
    @FXML
    private TextField adm_courseadd_tf_csn;
    @FXML
    private TextField adm_courseadd_tf_classroom;

    public void register() throws Exception {

        Alert alert = new Alert(Alert.AlertType.WARNING);
        String cid, cname, tid, ctime, csn,classroom;
        cid = adm_courseadd_tf_cid.getText();
        cname = adm_courseadd_tf_cname.getText();
        tid = adm_courseadd_tf_tid.getText();
        ctime = adm_courseadd_tf_ctime.getText();
        csn=adm_courseadd_tf_csn.getText();
        classroom = adm_courseadd_tf_classroom.getText();
        String sql = MessageFormat.format("Insert into  course values ({0}{1}{0},{0}{2}{0},{0}{3}{0},{0}{4}{0},{0}{5}{0},{0}{6}{0})", '"', cid, cname, tid, ctime, csn,classroom);
        JDBC_T jdbc_t = new JDBC_T();
        if (jdbc_t.is_conn()) {
            try {
                PreparedStatement preparedStatement = jdbc_t.getConn().prepareStatement(sql);
                System.out.println(sql);
                preparedStatement.execute(sql);
                alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setContentText("注册成功!!!");
                alert.showAndWait();
            } catch (SQLException sqlException) {
                alert = new Alert(Alert.AlertType.WARNING);
                alert.setContentText("插入数据表失败!!!请检查后重新插入");
                alert.showAndWait();
            }

        }
    }
}